package com.gzhryc.shared_device.oem.h5.mng.controllers.config;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.config.dao.db.LogisticsCompany;
import com.gzhryc.shared_device.oem.code.config.dao.enums.LogisticsCompanyEnum;
import com.gzhryc.shared_device.oem.code.config.services.LogisticsCompanyService;
import com.gzhryc.shared_device.oem.code.config.services.dto.LogisticsCompanySearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class LogisticsCompanyController {

    @Route(value = { "/config/logistics_company/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", LogisticsCompanyEnum.State.values());
        return "/config/logistics_company/list.html";
    }

    @Route(value = "/config/logistics_company/list.data")
    public ListResult<LogisticsCompany> listData(HttpServletRequest request) {
        ListResult<LogisticsCompany> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        LogisticsCompanySearch search = WebUtils.getParameter(request,LogisticsCompanySearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(LogisticsCompanyService.self().countBySearch(search));
        List<LogisticsCompany> logisticsCompanyList = LogisticsCompanyService.self().findBySearch(search,pageNo,pageLimit);
        if(logisticsCompanyList != null && logisticsCompanyList.size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(logisticsCompanyList);
        }
        return result;
    }

    @Route(value = { "/config/logistics_company/add.html" })
    public String add(HttpServletRequest request) {
        request.setAttribute("stateList", LogisticsCompanyEnum.State.values());
        return "/config/logistics_company/edit.html";
    }

    @Route(value = {"/config/logistics_company/add.do"})
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增失败，请稍后再试");
        LogisticsCompany logisticsCompany = WebUtils.getParameter(request,LogisticsCompany.class);
        if(LogisticsCompanyService.self().add(logisticsCompany)){
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }

    @Route(value = { "/config/logistics_company/update.html" })
    public String update(HttpServletRequest request) {
        String id = WebUtils.getString(request,"id");
        LogisticsCompany logisticsCompany = LogisticsCompanyService.self().getById(id);
        WebUtils.setAttribute(request,logisticsCompany);
        request.setAttribute("stateList", LogisticsCompanyEnum.State.values());
        return "/config/logistics_company/edit.html";
    }

    @Route(value = {"/config/logistics_company/update.do"})
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改失败，请稍后再试");
        LogisticsCompany logisticsCompany = WebUtils.getParameter(request,LogisticsCompany.class);
        if(LogisticsCompanyService.self().updateNotNull(logisticsCompany)){
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }

    @Route(value = {"/config/logistics_company/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败，请稍后再试");
        String id = WebUtils.getString(request,"id");
        if(LogisticsCompanyService.self().deleteById(id)){
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }
}
